package com.neihe.common.base;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常处理 配置
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ApiException.class)
    public ResultVO<String> ApiExceptionHandler(ApiException e) {
        // 这里传递的响应码枚举
        int code = e.getCode();
        return ObjectUtil.isNotNull(code) ? new ResultVO<>(code, e.getMessage()) : new ResultVO<>(e.getMessage());

    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultVO<String> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 这里传递的响应码枚举
        return new ResultVO<>(ResultCode.VALIDATE_FAILED, objectError.getDefaultMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public ResultVO<String> notLoginException(NotLoginException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        int errCode = e.getCode();
        /*
          11001	未能读取到有效Token
          11002	登录时的用户ID值为空
          11003	更改 Token 指向的 用户ID 时，用户ID值为空
          11011	未能读取到有效Token
          11012	Token无效
          11013	Token已过期
          11014	Token已被顶下线
          11015	Token已被踢下线
          11016	Token已被冻结
         */
        if (errCode == 11001 || errCode == 11011 || errCode == 11012 ||
                errCode == 11013 || errCode == 11014) {
            log.error("请求地址'{}',发生系统异常. 异常码 {}", requestURI, e.getCode());
            return new ResultVO<>(ResultCode.NOT_LOGIN_ERROR, e.getMessage());
        }

        log.error("请求地址'{}',发生系统异常. 异常码 {}", requestURI, e.getCode());
        return new ResultVO<>(ResultCode.ERROR, e.getMessage());
    }

    @ExceptionHandler(NotRoleException.class)
    public ResultVO<String> notRoleException(NotRoleException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        int errCode = e.getCode();
        /*
          11041	缺少指定的角色
          11051	缺少指定的权限
         */
        if (errCode == 11041 || errCode == 11051) {
            log.error("请求地址'{}',发生系统异常. 异常码 {}", requestURI, e.getCode());
            return new ResultVO<>(ResultCode.NO_AUTH_ERROR, e.getMessage());
        }

        log.error("请求地址'{}',发生系统异常. 异常码 {}", requestURI, e.getCode());
        return new ResultVO<>(ResultCode.ERROR, e.getMessage());
    }


    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public ResultVO<String> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        return new ResultVO<>(ResultCode.FAILED, e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public ResultVO<String> handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return new ResultVO<>(ResultCode.ERROR, e.getMessage());
    }

}
